package yl.hs.bmipfull.controllers.system;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IDutyInfoService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:  职位信息控制层<br/>
 * 创建时间: 2021年05月12日 15时51分48秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "职位信息Web接口", tags = "系统管理——职位信息操作")
@RequestMapping(value = "/api/organization")
@RestController
public class DutyInfoController extends ControllerBase {

    @Autowired
    IDutyInfoService service;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询职位信息", notes = "查询职位信息")
    @RequestMapping(value = "/duty/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryList(@RequestBody PageQuery pageQuery, @PathVariable int rowNum, @PathVariable int page) {
        try {
            svSVC.updateUserMenuView(pageQuery);
            return success("查询成功", service.queryList(pageQuery, rowNum, page));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询职位信息", notes = "查询职位信息")
    @RequestMapping(value = "/queryDuty", method = RequestMethod.GET)
    public WebResult queryAll() {
        try {
            return success("查询成功", service.queryAll());
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询职位信息", notes = "根据职位信息编号查询职位信息")
    @RequestMapping(value = "/duty/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }

    @ApiOperation(value = "查询职位信息", notes = "根据组织单位信息编号查询职位信息")
    @RequestMapping(value = "/queryDuty/{unitSN}", method = RequestMethod.GET)
    public WebResult querySelectList(@PathVariable String unitSN, @RequestParam(value = "key", required = false) String key) {
        var result = service.queryListByUnitSN(unitSN.split(","), key);
        return success("ok", result);
    }

    @ApiOperation(value = "保存职位信息", notes = "查询/更新职位信息信息")
    @RequestMapping(value = "/duty", method = RequestMethod.PUT)
    public WebResult saveMenu(@RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveDutyInfo(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除职位信息信息", notes = "根据职位信息编号删除系职位信息信息")
    @RequestMapping(path = "/duty", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "职位信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}